﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class DepSchedule : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void ibSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (txtDate.Text != "" && txtName.Text!="")
        {
            lblDateTime.Text = txtDate.Text;
            DateTime date = Convert.ToDateTime(txtDate.Text);
            User user = (User)Session["userManage"];
            //string depId = txtDep.Text;
            string depId = ds.SelectedText;
            string userId = txtName.Text;

            gvDepSchedule.DataSource = new MyScheduleBLL().GetSchedule( userId, depId, date);
            gvDepSchedule.DataBind();
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "key", "<script>alert('请输入要查找的部门、日期、姓名')</script>");
        }
    }
    protected void gvDepSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (txtDate.Text != "")
        {
            string date = txtDate.Text + " 00:00:00";//得到选择的日期
            DateTime selectTime = Convert.ToDateTime(date);

            if (e.Row.RowType == DataControlRowType.Header)//判断行类型是不是标头
            {
                //循环显示一周中各天
                for (int i = 1; i <= 7; i++)
                {
                    int week = i - Convert.ToInt32(selectTime.DayOfWeek) - 1;
                    string strWeek = selectTime.AddDays(week).ToString("yyyy-MM-dd"); //一周各天所对应的日期
                    DateTime weekDay = DateTime.Parse(strWeek + " 00:00:00");

                    if (weekDay.Month.ToString() == selectTime.Month.ToString())
                    {
                        Label lblWeek = (Label)e.Row.FindControl("lbl" + i + "");//实例化标题控件
                        lblWeek.Text = weekDay.Day.ToString();
                    }
                }

            }
            if (e.Row.RowType == DataControlRowType.DataRow)//判断行类型是不是数据行
            {
                string userName = gvDepSchedule.DataKeys[e.Row.RowIndex].Value.ToString();
                List<v_UserDepPosition> user = new UserManager().ShowUserInfoByName(userName);
                foreach (var item in user)
                {
                   e.Row.Cells[0].Text = item.UserId; 
                }
                

                //循环显示一周中各天所对应的日程内容
                for (int i = 1; i <= 7; i++)
                {
                    int intWeek = i - Convert.ToInt32(selectTime.DayOfWeek);
                    string strWeek = selectTime.AddDays(intWeek).ToString("yyyy-MM-dd");   //星期几对应的日期
                    foreach (var item in user)
                    {
                        int userNo = item.UserNo;
                        DateTime dt = DateTime.Parse(strWeek+ " 00:00:00");
                       int scheduleNo = new MyScheduleBLL().GetScheduleNo(userNo,dt );
                     
                        //根据用户Id和日期获得日程Id
                        if (scheduleNo != 0)
                        {
                            List<Schedule> schedule = new MyScheduleBLL().ShowScheduleByNo(scheduleNo);  //根据日程Id获得日程信息              
                            HyperLink hl = (HyperLink)e.Row.FindControl("hl" + i + "");//实例化内容控件
                            //指定HyperLink控件的文本属性
                            hl.Text = "◎" + schedule[0].SchTopic.Substring(0, 3) + "...";
                            hl.Attributes.Add("style","text-decoration:none;");
                            //指定HyperLink控件的超连接属性
                            hl.NavigateUrl = "../Schedule/SaveMySchedule.aspx?readonly='true'&userid=" + userName + "&today=" + selectTime.ToShortDateString();
                        }
                    }
                }
            }
        }
    }
}